home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / language / china-util.el.z / china-util.el
Encoding:
Text File  |  1998-05-21  |  7.7 KB  |  247 lines

  1. ;;; china-util.el --- utilities for Chinese
  2.  
  3. ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
  4. ;; Licensed to the Free Software Foundation.
  5. ;; Copyright (C) 1997 MORIOKA Tomohiko
  6.  
  7. ;; Keywords: mule, multilingual, Chinese
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Code:
  27.  
  28. ;;;###autoload
  29. (defun setup-chinese-gb-environment ()
  30.   "Setup multilingual environment (MULE) for Chinese GB2312 users."
  31.   (interactive)
  32.   (setup-english-environment)
  33.  
  34.   (set-default-coding-systems 'cn-gb-2312)
  35.   ;; (setq coding-category-iso-8-2 'chinese-iso-8bit)
  36.   (set-coding-category-system 'iso-8-2 'cn-gb-2312)
  37.   ;; (setq coding-category-iso-7-else 'chinese-iso-7bit)
  38.   ;; (setq coding-category-big5 'chinese-big5)
  39.  
  40.   ;; (set-coding-priority
  41.   ;;  '(coding-category-iso-7
  42.   ;;    coding-category-iso-7-else
  43.   ;;    coding-category-iso-8-2
  44.   ;;    coding-category-big5
  45.   ;;    coding-category-iso-8-1
  46.   ;;    coding-category-emacs-mule
  47.   ;;    coding-category-iso-8-else))
  48.   (set-coding-priority-list
  49.    '(iso-7
  50.      iso-8-2
  51.      big5
  52.      iso-8-1
  53.      no-conversion
  54.      iso-8-designate
  55.      iso-lock-shift
  56.      shift-jis))
  57.   
  58.   ;; (when (eq 'x (device-type (selected-device)))
  59.   ;;   (x-use-halfwidth-roman-font 'chinese-gb2312 "gb1988"))))
  60.   (when (featurep 'egg)
  61.     (setq-default its:*current-map* (its:get-mode-map "PinYin")))
  62.   (setq default-input-method  "chinese-py-punct")
  63.   )
  64.  
  65. ;;;###autoload
  66. (defun setup-chinese-big5-environment ()
  67.   "Setup multilingual environment (MULE) for Chinese Big5 users."
  68.   (interactive)
  69.   (setup-english-environment)
  70.  
  71.   (set-default-coding-systems 'big5)
  72.   ;; (setq coding-category-iso-8-2 'chinese-big5)
  73.   (set-coding-category-system 'iso-8-2 'cn-gb-2312)
  74.   ;; (setq coding-category-iso-7-else 'chinese-iso-7bit)
  75.   ;; (setq coding-category-big5 'chinese-big5)
  76.  
  77.   ;; (set-coding-priority
  78.   ;;  '(coding-category-iso-7
  79.   ;;    coding-category-iso-7-else
  80.   ;;    coding-category-big5
  81.   ;;    coding-category-iso-8-2
  82.   ;;    coding-category-emacs-mule
  83.   ;;    coding-category-iso-8-else))
  84.   (set-coding-priority-list
  85.    '(iso-7
  86.      big5
  87.      iso-8-2
  88.      no-conversion
  89.      iso-8-1
  90.      iso-8-designate
  91.      iso-lock-shift
  92.      shift-jis))
  93.   
  94.   (setq default-input-method "chinese-py-punct-b5")
  95.   )
  96.  
  97. ;; ;;;###autoload
  98. ;; (defun setup-chinese-cns-environment ()
  99. ;;   "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
  100. ;;   (interactive)
  101. ;;   (setup-english-environment)
  102. ;; 
  103. ;;   (setq coding-category-iso-7-else 'chinese-iso-7bit)
  104. ;;   (setq coding-category-big5 'chinese-big5)
  105. ;;   (setq coding-category-iso-8-2 'chinese-big5)
  106. ;;   (set-default-coding-systems 'chinese-iso-7bit)
  107. ;; 
  108. ;;   (set-coding-priority
  109. ;;    '(coding-category-iso-7
  110. ;;      coding-category-iso-7-else
  111. ;;      coding-category-iso-8-2
  112. ;;      coding-category-big5
  113. ;;      coding-category-iso-7-else))
  114. ;; 
  115. ;;   (setq-default buffer-file-coding-system 'chinese-iso-7bit)
  116. ;;   (setq default-terminal-coding-system 'chinese-iso-7bit)
  117. ;;   (setq default-keyboard-coding-system 'chinese-iso-7bit)
  118. ;; 
  119. ;;   (setq default-input-method "chinese-quick-cns"))
  120.  
  121. ;; Hz/ZW encoding stuffs
  122.  
  123. ;; HZ is an encoding method for Chinese character set GB2312 used
  124. ;; widely in Internet.  It is very similar to 7-bit environment of
  125. ;; ISO-2022.  The difference is that HZ uses the sequence "~{" and
  126. ;; "~}" for designating GB2312 and ASCII respectively, hence, it
  127. ;; doesn't uses ESC (0x1B) code.
  128.  
  129. ;; ZW is another encoding method for Chinese character set GB2312.  It
  130. ;; encodes Chinese characters line by line by starting each line with
  131. ;; the sequence "zW".  It also uses only 7-bit as HZ.
  132.  
  133. ;; ISO-2022 escape sequence to designate GB2312.
  134. (defvar iso2022-gb-designation "\e$A")
  135. ;; HZ escape sequence to designate GB2312.
  136. (defvar hz-gb-designnation "~{")
  137. ;; ISO-2022 escape sequence to designate ASCII.
  138. (defvar iso2022-ascii-designation "\e(B")
  139. ;; HZ escape sequence to designate ASCII.
  140. (defvar hz-ascii-designnation "~}")
  141. ;; Regexp of ZW sequence to start GB2312.
  142. (defvar zw-start-gb "^zW")
  143. ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
  144. (defvar hz/zw-start-gb (concat hz-gb-designnation "\\|" zw-start-gb))
  145.  
  146. (defvar decode-hz-line-continuation nil
  147.   "Flag to tell if we should care line continuation convention of Hz.")
  148.  
  149. ;;;###autoload
  150. (defun decode-hz-region (beg end)
  151.   "Decode HZ/ZW encoded text in the current region.
  152. Return the length of resulting text."
  153.   (interactive "r")
  154.   (save-excursion
  155.     (save-restriction
  156.       (narrow-to-region beg end)
  157.  
  158.       ;; We, at first, convert HZ/ZW to `iso-2022-7bit',
  159.       ;; then decode it.
  160.  
  161.       ;; "~\n" -> "\n"
  162.       (goto-char (point-min))
  163.       (while (search-forward "~" nil t)
  164.     (if (= (following-char) ?\n) (delete-char -1))
  165.     (if (not (eobp)) (forward-char 1)))
  166.  
  167.       ;; "^zW...\n" -> Chinese GB2312
  168.       ;; "~{...~}"  -> Chinese GB2312
  169.       (goto-char (point-min))
  170.       (let ((chinese-found nil))
  171.     (while (re-search-forward hz/zw-start-gb nil t)
  172.       (if (= (char-after (match-beginning 0)) ?z)
  173.           ;; ZW -> iso-2022-7bit
  174.           (progn
  175.         (delete-char -2)
  176.         (insert iso2022-gb-designation)
  177.         (end-of-line)
  178.         (insert iso2022-ascii-designation))
  179.         ;; HZ -> iso-2022-7bit
  180.         (delete-char -2)
  181.         (insert iso2022-gb-designation)
  182.         (let ((pos (save-excursion (end-of-line) (point))))
  183.           (if (search-forward hz-ascii-designnation pos t)
  184.           (replace-match iso2022-ascii-designation)
  185.         (if (not decode-hz-line-continuation)
  186.             (insert iso2022-ascii-designation)))))
  187.       (setq chinese-found t))
  188.     (if (or chinese-found
  189.         (let ((enable-multibyte-characters nil))
  190.           ;; Here we check if the text contains EUC (China) codes.
  191.           ;; If any, we had better decode them also.
  192.           (goto-char (point-min))
  193.           (re-search-forward "[\240-\377]" nil t))) 
  194.         (decode-coding-region (point-min) (point-max) 'euc-china)))
  195.  
  196.       ;; "~~" -> "~"
  197.       (goto-char (point-min))
  198.       (while (search-forward "~~" nil t) (delete-char -1))
  199.       (- (point-max) (point-min)))))
  200.  
  201. ;;;###autoload
  202. (defun decode-hz-buffer ()
  203.   "Decode HZ/ZW encoded text in the current buffer."
  204.   (interactive)
  205.   (decode-hz-region (point-min) (point-max)))
  206.  
  207. ;;;###autoload
  208. (defun encode-hz-region (beg end)
  209.   "Encode the text in the current region to HZ.
  210. Return the length of resulting text."
  211.   (interactive "r")
  212.   (save-excursion
  213.     (save-restriction
  214.       (narrow-to-region beg end)
  215.  
  216.       ;; "~" -> "~~"
  217.       (goto-char (point-min))
  218.       (while (search-forward "~" nil t)    (insert ?~))
  219.  
  220.       ;; Chinese GB2312 -> "~{...~}"
  221.       (goto-char (point-min))
  222.       (if (re-search-forward "\\cc" nil t)
  223.       (let ((enable-multibyte-characters nil)
  224.         pos)
  225.         (goto-char (setq pos (match-beginning 0)))
  226.         (encode-coding-region pos (point-max) 'iso-2022-7bit)
  227.         (goto-char pos)
  228.         (while (search-forward iso2022-gb-designation nil t)
  229.           (delete-char -3)
  230.           (insert hz-gb-designnation))
  231.         (goto-char pos)
  232.         (while (search-forward iso2022-ascii-designation nil t)
  233.           (delete-char -3)
  234.           (insert hz-ascii-designnation))))
  235.       (- (point-max) (point-min)))))
  236.  
  237. ;;;###autoload
  238. (defun encode-hz-buffer ()
  239.   "Encode the text in the current buffer to HZ."
  240.   (interactive)
  241.   (encode-hz-region (point-min) (point-max)))
  242.  
  243. ;;
  244. (provide 'china-util)
  245.  
  246. ;;; china-util.el ends here
  247.